<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>发送 POST 请求</title>
    <style>
       .box{
           width: 300px;
           height: 300px;
           border:solid 1px #999;
       }
    </style>
</head>
<body>
    <div class="box"></div>
    <script>
        //获取 div
        let box = document.querySelector('.box');

        //绑定事件
        box.onmouseenter = function(){
            //1. 创建请求对象
            const xhr = new XMLHttpRequest();

            //2. 设置请求行
            xhr.open('post', 'http://127.0.0.1/server'); // id=100

            // AJAX 设置请求头
            // xhr.setRequestHeader('id', 100);// set 设置  request 请求  header 头

            //3. 设置请求体
            //JSON 格式
            // xhr.setRequestHeader('Content-type', 'application/json');
            // xhr.send('{"id":100, "name":"张三"}');

            //query string 形式
            xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');
            xhr.send('id=100&name=zhangsan');
            
            //4. 处理结果
            xhr.onreadystatechange = function(){
                //判断
                if(xhr.readyState === 4){
                    // 处理响应结果
                    // xhr.response;
                    box.innerHTML = xhr.response;
                }
            }
        }
    </script>
</body>
</html>